package com.anuprayog.convertunits;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;

public class MassActivity extends Activity {

	private EditText text, result;
	private Spinner spin1, spin2;
	private TextView resultString, resultString1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mass);

		text = (EditText) findViewById(R.id.editText1);
		spin1 = (Spinner) findViewById(R.id.spinner1);
		spin2 = (Spinner) findViewById(R.id.spinner2);
		result = (EditText) findViewById(R.id.editText2);
		resultString = (TextView) findViewById(R.id.textView4);
		resultString1 = (TextView) findViewById(R.id.textView5);
	}

	/**
	 * Set up the {@link android.app.ActionBar}, if the API is available.
	 */
	/*@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}*/

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.mass, menu);
		return true;
	}

	/*@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}*/
	
	public void onClick(View view) {
		if (text.getText().length() == 0) {
			Toast.makeText(this, "Please enter a valid number",
					Toast.LENGTH_LONG).show();
			return;
		}

		double input = Double.parseDouble(text.getText().toString());
		String unit1 = spin1.getSelectedItem().toString();
		String unit2 = spin2.getSelectedItem().toString();
		double gramvalue;
		double d;
		
		if (unit1.equals("Microgram"))
		{
			gramvalue = input*(0.000001);
		}
		
		else if (unit1.equals("Milligram"))
		{
			gramvalue = input*(0.001);
		}
		
		else if (unit1.equals("Gram"))
		{
			gramvalue = input;
		}
		
		else if (unit1.equals("Kilogram"))
		{
			gramvalue = input*(1000);
		}
		
		else if (unit1.equals("Metric Ton"))
		{
			gramvalue = input*(1000000);
		}
		
		else if (unit1.equals("AMU"))
		{
			gramvalue = input*(1.66053873E-27);
		}
		
		else if (unit1.equals("Hyl"))
		{
			gramvalue = input*(9806.65);
		}
		
		else if (unit1.equals("Carat"))
		{
			gramvalue = input*(0.2);
		}
		
		else if (unit1.equals("Grain"))
		{
			gramvalue = input*(0.06479891);
		}
		
		else if (unit1.equals("Pennyweight"))
		{
			gramvalue = input*(1.55517384);
		}
		
		else if (unit1.equals("Dram"))
		{
			gramvalue = input*(1.7718451953125);
		}
		
		else if (unit1.equals("Ounce"))
		{
			gramvalue = input*(28.349523125);
		}
		
		else if (unit1.equals("Pound"))
		{
			gramvalue = input*(453.59237);
		}
		
		else if (unit1.equals("Stone"))
		{
			gramvalue = input*(6350.29318);
		}
		
		else if (unit1.equals("Long Quarter"))
		{
			gramvalue = input*(12700.58636);
		}
		
		else if (unit1.equals("Long Hundredweight"))
		{
			gramvalue = input*(50802.34544);
		}
		
		else if (unit1.equals("Long Ton"))
		{
			gramvalue = input*(1016046.9088);
		}
		
		else if (unit1.equals("Short Quarter"))
		{
			gramvalue = input*(11339.80925);
		}
		
		else if (unit1.equals("Short Hundredweight"))
		{
			gramvalue = input*(45359.237);
		}
		
		else if (unit1.equals("Short Ton"))
		{
			gramvalue = input*(907184.74);
		}
		
		else if (unit1.equals("Firkin"))
		{
			gramvalue = input*(40823.3133);
		}
		
		else if (unit1.equals("Troy Ounce"))
		{
			gramvalue = input*(31.1034768);
		}
		
		else if (unit1.equals("Troy Pound"))
		{
			gramvalue = input*(373.2417216);
		}
		
		else if (unit1.equals("Tola"))
		{
			gramvalue = input*(11.66375);
		}
		
		else if (unit1.equals("Ser"))
		{
			gramvalue = input*(933.1);
		}
		
		else if (unit1.equals("Maund"))
		{
			gramvalue = input*(37324);
		}
		
		else if (unit1.equals("Pav"))
		{
			gramvalue = input*(250);
		}
		
		else
		{
			gramvalue = 0;
		}		
		
		if (unit2.equals("Microgram")){
			d = gramvalue*(1000000);
		}
		
		else if (unit2.equals("Milligram")){
			d = gramvalue*(1000);
		}
		
		else if (unit2.equals("Gram")){
			d = gramvalue;
		}
		
		else if (unit2.equals("Kilogram")){
			d = gramvalue*(0.001);
		}
		
		else if (unit2.equals("Metric Ton")){
			d = gramvalue*(0.000001);
		}
		
		else if (unit2.equals("AMU"))
		{
			d = gramvalue*(602214198280096724994785276.70354);
		}
	
		else if (unit2.equals("Hyl")){
			d = gramvalue*(1/9806.65);
		}
		
		else if (unit2.equals("Carat")){
			d = gramvalue*(5);
		}
		
		else if (unit2.equals("Grain")){
			d = gramvalue*(1/0.06479891);
		}
		
		else if (unit2.equals("Pennyweight")){
			d = gramvalue*(1/1.55517384);
		}
		
		else if (unit2.equals("Dram")){
			d = gramvalue*(1/1.7718451953125);
		}
		
		else if (unit2.equals("Ounce")){
			d = gramvalue*(1/28.349523125);
		}
		
		else if (unit2.equals("Pound")){
			d = gramvalue*(1/453.59237);
		}
		
		else if (unit2.equals("Stone")){
			d = gramvalue*(1/6350.29318);
		}
		
		else if (unit2.equals("Long Quarter")){
			d = gramvalue*(1/12700.58636);
		}
		
		else if (unit2.equals("Long Hundredweight")){
			d = gramvalue*(1/50802.34544);
		}
		
		else if (unit2.equals("Long Ton")){
			d = gramvalue*(1/1016046.9088);
		}
		
		else if (unit2.equals("Short Quarter")){
			d = gramvalue*(1/11339.80925);
		}
		
		else if (unit2.equals("Short Hundredweight")){
			d = gramvalue*(1/45359.237);
		}
		
		else if (unit2.equals("Short Ton")){
			d = gramvalue*(1/907184.74);
		}
		
		else if (unit2.equals("Firkin")){
			d = gramvalue*(1/40823.3133);
		}
		
		else if (unit2.equals("Troy Ounce")){
			d = gramvalue*(1/31.1034768);
		}
		
		else if (unit2.equals("Troy Pound")){
			d = gramvalue*(1/373.2417216);
		}
		
		else if (unit2.equals("Tola")){
			d = gramvalue*(1/11.66375);
		}
		
		else if (unit2.equals("Ser")){
			d = gramvalue*(1/933.1);
		}
		
		else if (unit2.equals("Maund")){
			d = gramvalue*(1/37324d);
		}
		
		else if (unit2.equals("Pav")){
			d = gramvalue*(0.004);
		}
		
		else
		{
			d = 0;
		}
	
		result.setText(String.valueOf(d));
		
		resultString.setText(String.valueOf(input) + " " + unit1 + " =");
		resultString1.setText(String.valueOf(d) + " " + unit2);
		
	}
	
	public void goBack(View view) {
		finish();
	}
		
	public void goSwap(View view) {
		int spin1Index = spin1.getSelectedItemPosition();

		spin1.setSelection(spin2.getSelectedItemPosition());
		spin2.setSelection(spin1Index);
	}

}
